<template>
  <div class="forgot-password-container">
    <div class="forgot-password-box">
      <h2>找回密码</h2>
      <el-form :model="form" :rules="rules" ref="forgotPasswordForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="User"/>
        </el-form-item>
        <el-form-item prop="passwordHash">
          <el-input v-model="form.passwordHash" type="password" placeholder="请输入新密码" prefix-icon="Lock"
                    show-password/>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="form.email" placeholder="请输入注册邮箱" prefix-icon="Message"/>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" prefix-icon="Phone"/>
        </el-form-item>
        <div class="form-actions">
          <el-button type="primary" :style="{backgroundColor: '#4f46e5'}" @click="handleSubmit" class="submit-btn">
            发送重置链接
          </el-button>
        </div>
        <div class="form-links">
          <router-link to="/login" class="link">返回登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import fetch from '@/api/fetch'

export default {
  name: 'ForgotPassword',
  data() {
    return {
      form: {
        email: '',
        username: '',
        passwordHash: '',
        phone: ''
      },
      rules: {
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
        ],
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        passwordHash: [{required: true, message: '请输入新密码', trigger: 'blur'}],
        phone: [{required: true, message: '请输入手机号码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    async handleSubmit() {
      this.$refs.forgotPasswordForm.validate(async valid => {
        if (valid) {
          try {
            fetch('/client-user/user/pc/forgetPassword', this.form, {method: 'post'}).then((res) => {
              console.log(res)
              if (res) {
                if (res.code === '200' && res.result) {
                  this.$message.success('密码重置成功')
                  this.$router.push('/login')
                }
              }
            })
          } catch (error) {
            this.$message.error(error.response?.data?.message || '密码重置失败，请重试')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f3f4f6;
  padding: 1rem;
}

.forgot-password-box {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: #4f46e5;
  margin-bottom: 2rem;
  font-size: clamp(1.5rem, 4vw, 2rem);
}

.form-actions {
  margin-top: 1.5rem;
}

.submit-btn {
  width: 100%;
  padding: 0.75rem;
  font-size: clamp(0.875rem, 2vw, 1rem);
}

.form-links {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}

.link {
  color: #4f46e5;
  text-decoration: none;
  font-size: clamp(0.75rem, 2vw, 0.875rem);
}

.link:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .forgot-password-box {
    padding: 1.5rem;
  }

  .form-links {
    justify-content: center;
  }

  .link {
    margin: 0 0.5rem;
  }
}
</style>